var config = require('config');
cc.Class({
    extends: cc.Component,

    properties: {
        money: cc.node,
        score: cc.node,
        noScore: cc.node,
        bg: cc.node,
        moneyAudio:{
            default: null,
            url: cc.AudioClip
        },
        boom: {
            default: null,
            serializable: false
        }
    },

    // use this for initialization
    onLoad: function () {
        this.bg = this.node.getChildByName("confirmbg");
        this.money = this.node.getChildByName('money');
        this.score = this.node.getChildByName('score');
        this.noScore = this.node.getChildByName('noscore');
        this.node.on(cc.Node.EventType.TOUCH_END, function () {
            
            // this.getdatafromSer();
            this.showMoney();
        }, this);
        
    },
    
    showMoney: function() {
        // this.getdatafromSer();
        var vs = cc.director.getVisibleSize();
        var moveto = cc.moveTo(1.5, cc.p(0, vs.height/2 -100))
        var movemoney = cc.moveTo(1.5, cc.p(0, -1600));
        var fade = cc.fadeOut(1);
        var seq = cc.sequence(moveto,fade ,cc.callFunc(this.closeConfirm, this));
        this.bg.runAction(cc.hide());
        this.money.runAction(movemoney);
        cc.audioEngine.playEffect(this.moneyAudio, false);
        this.score.runAction(seq);
        this.boom.startGame();
    },
    getdatafromSer: function(){
        var self = this;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var response = xhr.responseText;
                console.log("draw : " +response);
                var res = JSON.parse(response );  
                if(res['code'] < 0){
                    self.noScore.opacity = 255;
                    self.noScore.runAction(cc.fadeOut(2));
                }else{
                    cc.log(res['data']['card']);
                    cc.log(res['data']['url']);
                    config.cardid = res['data']['card'];
                    config.cardurl = res['data']['url'];
                    self.showMoney();
                }
            }
        };
        // http://mysite/drawserver/drawserver.php
        // url....com/draw/server/drawserver.php
        var userid = config.userid;
        // var testuid = 100000009054118;
        xhr.open("GET", "url....com/draw/server/drawserver.php?userid="+userid, true);
        xhr.send();
    },
    
    closeConfirm: function() {
        var seq = cc.sequence(cc.hide(),cc.moveBy(0.1, cc.p(1000,1500)));
        this.node.runAction(seq);
        this.boom.startMove = true;
    },
    
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
